// pages/detail/detail.js
const app = getApp()
import { route } from '../../utils/util'
let that;
Page({
  data: {
    showStat: false,
    list_data: [],
    date_ary: new Array(),
    date_obj: {}
  },
  onLoad: function (options) {
    
  },
  onShow: function () {
    that = this;
    let ui = wx.getStorageSync('userInfo');
    that.setData({ ui })
    if (!ui) {
      return that.setData({ showStat: false })
    }
    that.getList()
  },
  getList() {
    that = this;
    let ui = that.data.ui;
    let list_data = that.data.list_data;
    let date_obj = that.data.date_obj;
    let showStat = that.data.showStat;
    var date_ary = that.data.date_ary
    if (!ui.openid) {
      route('/pages/mine/mine', {})
    } else {
      wx.showLoading({
        title: '粪力加载',
      })
      wx.cloud.callFunction({
        name: 'getrecord',
        data: {
          openid: ui.openid
        },
        success: res => {
          if(res.result!=null){
            list_data = res.result.data;
            list_data.forEach(item => {
              // 格式化 分 秒
              item.minutes = item.minutes < 10 && item.minutes.length != 2 ? '0' + item.minutes : item.minutes;
              item.seconds = item.seconds < 10 && item.seconds.length != 2 ? '0' + item.seconds : item.seconds;
              // 数据处理
              date_ary.push(item.sleDate) // 日期 存入 日期数组中
              // 日期数组 去重 排序 
              date_ary = Array.from(new Set(date_ary)).sort().reverse()
              let ary = []
              if (Object.keys(date_obj).length == 0) { // 空对象
                date_obj[item.sleDate] = new Array(item)
              } else { // 不是空对象
                if (!date_obj[item.sleDate]) { // 没有此key
                  date_obj[item.sleDate] = new Array(item)
                } else { // 有此key
                  ary = date_obj[item.sleDate]
                  ary.push(item)
                  date_obj[item.sleDate] = ary
                }
              }
            })
            wx.hideLoading()
            console.log('获取粑粑日记成功');
            that.setData({ showStat:true, total: list_data.length, date_obj, date_ary })
          }else{
            wx.hideLoading()
            that.setData({showStat:false})
          }
        }, fail: err => {
          console.log(err);
        }
      })
    }
  },
})